package com.tanhua.sso.service;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.tanhua.sso.config.AliyunSMSConfig;
import com.tanhua.sso.vo.ErrorResult;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.time.Duration;

@Service
@Log4j2
public class SmsService {
    @Autowired
    private AliyunSMSConfig aliyunSMSConfig;
    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    public String sendSms(String mobile) {
        DefaultProfile profile = DefaultProfile.getProfile(this.aliyunSMSConfig.getRegionId(),
                this.aliyunSMSConfig.getAccessKeyId(), this.aliyunSMSConfig.getAccessKeySecret());
        IAcsClient client = new DefaultAcsClient(profile);
       String code = RandomUtils.nextInt(100000, 999999) + "";
        //String code="123456";
        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain(this.aliyunSMSConfig.getDomian());
        request.setSysVersion("2017-05-25");
        request.setSysAction("SmsService");
        request.putQueryParameter("RegionId", this.aliyunSMSConfig.getRegionId());
        request.putQueryParameter("PhoneNumbers", "mobile"); //目标手机号
        request.putQueryParameter("SignName", this.aliyunSMSConfig.getSignName()); //签名名称
        request.putQueryParameter("TemplateCode", this.aliyunSMSConfig.getTemplateCode()); //短信模板code
        request.putQueryParameter("TemplateParam", "{\"code\":\"" + code + "\"}");//模板中变量替换
        try {
            //如果返回结果中包含有 "Message":"OK"
            CommonResponse response = client.getCommonResponse(request);
            if (StringUtils.contains(response.getData(), "\"Message\":\"OK\"")) {
                return code;
            }
            //这是返回数据  {"Message":"OK","RequestId":"EC2D4C9A-0EAC-4213-BE45-CE6176E1DF23","BizId":"110903802851113360^0","Code":"OK"}
            System.out.println(response.getData());
        } catch (Exception e) {
            log.error("发送短信验证失败~mobile=" + mobile, e);
            // e.printStackTrace();
        }

        return null;
    }


    //发送短信验证码 把验证码保存在errprResult中
    public ErrorResult sendChecCode(String phone) {
        String redisKey = "CHECK_CODE" + phone;
        //先判断该手机号发送的验证码是否过期 haskey方法判断是含有这个字符串
        if (this.redisTemplate.hasKey(redisKey)) {
            String msg = "上一次发送的验证码为失效,请输入之前的验证码";
            //使用build()方法进行创建对象
            return ErrorResult.builder().errCode("000001").errMessage(msg).build();
        }
//要是没有这上面的判断不会执行,自然会执行下面的发送验证码 并返回信息
       // String code = this.sendSms(phone);
        String code="123456";
        if (StringUtils.isEmpty(code)) {
            String msg = "发送短信验证码失败";
            return ErrorResult.builder().errCode("000000").errMessage(msg).build();
        }
        //执行完上面的 短信发送成功 将验证码保存到redis中 有效期为5分钟
        this.redisTemplate.opsForValue().set(redisKey, code, Duration.ofMinutes(50));
        return null;
    }

}
